package net.beyondts.example.mplug.plugin;

import com.alibaba.fastjson.JSON;
import net.beyondts.mplug.Self;
import net.beyondts.mplug.SpringBeansInvocation;
import net.beyondts.mplug.annotation.Plugin;
import net.beyondts.mplug.utils.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.InvocationTargetException;

/**
 * Project: mplug-project<br/>
 * Description: <br/>
 * Created at: 2019-1-12<br/>
 * Owned by beyondts.
 *
 * @author blazecrystal
 * @version 1.0.0
 */
@Component
public class PluginForSomeService extends Self {
    @Autowired
    private SomeComponent comp;

    @Plugin(extension = "void net.beyondts.example.mplug.extension.SomeService.print()", choose = "b:someComponent.condition()", compare = "true")
    public void pluginPrint() {
        System.out.println("--- this is plugin 1---");
    }

    @Plugin(extension = "void net.beyondts.example.mplug.extension.SomeService.print()", choose = "b:someComponent.condition()", compare = "false")
    public void pluginPrint2() {
        System.out.println("--- this is plugin 2---");
    }

    @Plugin(extension = "void net.beyondts.example.mplug.extension.SomeService.print(int)", choose = "a:x", compare = "100")
    public void pluginPrint(int x) {
        System.out.println("--- this is plugin with int input : " + x + " ----1");
    }

    @Plugin(extension = "void net.beyondts.example.mplug.extension.SomeService.print(int)", choose = "a:x", compare = "10")
    public void pluginPrint2(int y) {
        System.out.println("--- this is plugin with int input : " + y + " ----2");
    }

    @Plugin(extension = "void net.beyondts.example.mplug.extension.SomeService.print(String,int)")
    public void pluginPrint(String a, int b) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException, ClassNotFoundException {
        System.out.println("--- this is plugin string input : " + a + ", " + b + " ---");
        BeanFactory.getBean(PluginForSomeService.class).extensionInPlugin();
        invokeSelf("extensionInPlugin");
        String name = (String) SpringBeansInvocation.invokeByBeanName("someBean", "getName");
        int age = ((Integer) SpringBeansInvocation.invokeByBeanClass("net.beyondts.example.mplug.beans.SomeBean", "getAge")).intValue();
        System.out.println("person : " + name + ", " + age);
        System.out.println(JSON.toJSONString(SpringBeansInvocation.findByBeanName("someBean")));
        comp.iAmAnother();
    }

    @Plugin(extension = "String net.beyondts.example.mplug.extension.SomeService.test(String)")
    public String test(String x) {
        return x + "plugin----1";
    }

//    @Extension(id = "void net.beyondts.example.mplug.plugin.PluginForSomeService.extensionInPlugin()")
    public void extensionInPlugin() {
        System.out.println("this is an extension in plugin");
    }

    @Plugin(extension = "void net.beyondts.example.mplug.plugin.PluginForSomeService.extensionInPlugin()")
    public void pluginForExtensionInPlugin() {
        System.out.println("this is a plugin for the extension in plugin");
    }
}
